<?php

namespace App\Http\Controllers;

use App\Models\Expert;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;


class UserController extends Controller
{
    public function index(Request $request)
    {
        $oUsers=User::where('name','LIKE','%'.$request->name.'%')->paginate(5);
        $oUsers->appends(['name'=>$request->name]);
        return view('admin.user-list')
            ->with('oUsers',$oUsers);

//
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.add-user');//
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $user = $request->all();
        $rules=[
            'name'=>'required|alpha_dash|min:3|unique:users',
            'password'=>'required|min:6|confirmed',
        ];
        $message=[
            'name.required'=>'用户名不能为空',
            'name.alpha_dash'=>'用户名只能包含数字，字母，横线-或下划线_，不能包含其他字符',
            'name.min'=>'用户名太短，至少要有:min个字符',
            'name.unique'=>'用户名已存在，不能重复注册',
            'password.required'=>'密码不能为空',
            'password.min'=>'密码长度不能少于:min个字符',
            'password.confirmed'=>'两次密码输入不一致'
        ];

        $validate = Validator::make($user,$rules,$message);
        if($validate->fails()){
            $msg=$validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        $user['password']=Hash::make($user['password']);
        User::create($user);
        return json_encode(['success'=>true,'msg'=>'添加成功']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $user=User::find($id);
        return view('admin.edit-user')
            ->with('user',$user);//修改密码

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data=$request->all();
        $rules=[
            'password'=>'required|min:6|confirmed'
        ];
        $messages=[
            'password.required'=>'密码不能为空',
            'password.min'=>'密码不能少于:min位',
            'password.confirmed'=>'两次输入密码不一致'
        ];
        $validutor=Validator::make($data,$rules,$messages);
        if($validutor->fails()){
            $msg=$validutor->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        $data['password']=Hash::make($data['password']);
        $user=User::find($id);
        $user->password=$data['password'];
        $user->save();
        return json_encode(['success'=>true,'msg'=>"成功提交"]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id){
        $user=User::find($id);
        $user->delete();
        return redirect('/adm/user');
    }
    public function login(){
        return view("admin.login");
    }

    public function dologin(Request $request){//登录
        $name=$request->name;
        $password=$request->password;
        if(Auth::attempt(['name'=>$name,'password'=>$password]))
        {
            return json_encode(['success'=>true,'msg'=>'用户登录成功']);
        }
        else{
            return json_encode(['success'=>false,'msg'=>'用户登录错误，请重新登录']);
        }
    }

    public function logout(){//登出
        Auth::logout();
        return redirect('/login');

    }
}
